package com.viewpagerindicator.sample.fragents;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.viewpagerindicator.sample.R;
import com.viewpagerindicator.sample.adapters.GoodsListAdapter;
import com.viewpagerindicator.sample.beans.Goods;
import com.viewpagerindicator.sample.util.URLs;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

public final class ThirdFragment extends Fragment {
    private static final String KEY_CONTENT = "TestFragment:Content";

    public static ThirdFragment newInstance(String content) {
        ThirdFragment fragment = new ThirdFragment();
        return fragment;
    }

    private String mContent = "???";
    ArrayList<Goods> goodsArrayList=new ArrayList<>();
    private ListView listView;
    private GoodsListAdapter adapter;
    private View listviewBg; //列表试图
    private View progressingBg;//等待试图;
    private View errormsgBg;//错误试图;
    private String collectionIds="";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
            mContent = savedInstanceState.getString(KEY_CONTENT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.first_layout,container,false);
        listView= (ListView) view.findViewById(R.id.first_layout_listview);
        listviewBg=  view.findViewById(R.id.list_bg);
        progressingBg=  view.findViewById(R.id.progress_bg);
        errormsgBg=  view.findViewById(R.id.errormsg_bg);
        adapter=new GoodsListAdapter(getActivity(),R.layout.goods_listview_item,goodsArrayList);
        listView.setAdapter(adapter);
        errormsgBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData();
            }
        });
        initData();
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_CONTENT, mContent);
    }
    /**
     * 获取商品
     * */
    public void initData(){
        show(progressingBg);
        SharedPreferences mySharedPreferences= getActivity().getSharedPreferences("test", Activity.MODE_MULTI_PROCESS);
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        collectionIds=mySharedPreferences.getString("collections", "");
        if(collectionIds==null||collectionIds.equals("")){
            show(listviewBg);
            return;
        }
        collectionIds=collectionIds.replaceAll(",,",",");
        if(collectionIds.startsWith(",")){
            collectionIds=collectionIds.substring(1);
        }
        if(collectionIds.endsWith(",")){
            collectionIds=collectionIds.substring(0,collectionIds.lastIndexOf(","));
        }
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams requestParams=new RequestParams();
        requestParams.put("ids",collectionIds);
        client.get(URLs.listgoodsbyids,requestParams, new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                //解析数据
                try {
                    List<Goods> temp = Goods.parseList(responseBody);
                    if (temp != null) {
                        goodsArrayList.clear();
                        goodsArrayList.addAll(temp);
                        adapter.notifyDataSetChanged();
                    }
                    show(listviewBg);
                } catch (Exception e) {
                    Toast.makeText(getActivity(), getString(R.string.net_error), Toast.LENGTH_SHORT).show();
                    show(errormsgBg);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Toast.makeText(getActivity(), getString(R.string.net_error), Toast.LENGTH_SHORT).show();
                show(errormsgBg);
            }
        });
    }
    public void show(View view2show){
        errormsgBg.setVisibility(View.GONE);
        listviewBg.setVisibility(View.GONE);
        progressingBg.setVisibility(View.GONE);
        view2show.setVisibility(View.VISIBLE);
    }
}
